package com.sheep.classroompie.controller;


import com.sheep.classroompie.entity.Teacher;
import com.sheep.classroompie.service.TeacherService;
import com.sheep.classroompie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin("http://localhost:8080")
public class TeacherController {
  @Autowired
  TeacherService teacherService;
  @RequestMapping("/loginT")
  @ResponseBody
  public R<Teacher> login(@RequestBody Teacher teacher){
    Teacher teacherOne = teacherService.login(teacher);
    if(teacherOne == null || !teacherOne.getPassword().equals(teacher.getPassword())){
      return R.error("用户名或密码错误!");
    }
    return R.success(teacherOne);
  }
  @RequestMapping("/registerT")
  @ResponseBody
  public R<Teacher> register(@RequestBody Teacher teacher){
    Teacher teacherOne = new Teacher(teacher.getName(),teacher.getSchool(),teacher.getIdentity(),teacher.getUsername(),teacher.getPassword(), teacher.getProfilePicture());
    if(teacherService.register(teacherOne) != 0){
      return R.success(teacherOne);
    }
    return R.error("注册信息有误!");
  }

  @RequestMapping("/uploadAvatarT")
  @ResponseBody
  public Boolean uploadAvatar(@RequestBody Teacher teacher){
    return teacherService.uploadAvatar(teacher);
  }
  @RequestMapping("/changePasswordT")
  @ResponseBody
  public void changePasswordT(@RequestBody Teacher teacher){
    teacherService.changePassword(teacher);
  }
}
